<?php

class StoryAction extends Action {
	
    public function index(){
    	// 查询条件
    	$condition=[];
        if(!empty($_REQUEST['search_content'])){
            $condition['story_id']=array('EQ',$_REQUEST['search_content']);
            $condition['story_title']=array('LIKE','%'.$_REQUEST['search_content'].'%');
            $condition['story_dev']=array('EQ',$_REQUEST['search_content']);
            $condition['story_qat_tester']=array('EQ',$_REQUEST['search_content']);
            $condition['_logic']='OR';
        }

        $size=10;
            // 总条数
        $total_counts=D('Story')->getCounts($condition);
            //总页数
        $total_pages=ceil($total_counts/$size);
        $current_p=isset($_REQUEST['p'])?$_REQUEST['p']:1;
        $start=($current_p-1)*$size;
        $previous_p=1;
        $previous_disable=true;
        if($current_p>1){
            $previous_p=$current_p-1;
            $previous_disable=false;
        }
        $next_p=$total_pages;
        $next_disable=true;
        if($current_p<$total_pages){
            $next_p=$current_p+1;
            $next_disable=false;
        }    	
    	
        // 获取进度
        $progresses = D('Story')->getProgresses();
        $stories = D('Story')->getStories($condition,$start,$size);

        $this->assign('next_p',$next_p);
        $this->assign('next_disable',$next_disable);
        $this->assign('previous_p',$previous_p);
        $this->assign('previous_disable',$previous_disable);
        $this->assign('current_p',$current_p);
        $this->assign('total_pages',$total_pages);
        $this->assign('total_counts',$total_counts);
        $this->assign('devs',D('Dev')->getDevs(['org'=>'zc']));
        $this->assign('tests',D('Dev')->getDevs(['org'=>'cs']));
        $this->assign('progresses',$progresses);

    	$this->assign('stories',$stories);
    	$this->display('Story/index');
    }

    public function ajaxDoDel(){
        $result=array(
            'status'=>0,
            'data'=>'数据不存在',
            );
        if(empty($_REQUEST['id'])){
            $this->ajaxReturn($result);
        }

    	$condition['id']=$_REQUEST['id'];	

    	if(!D('Story')->delStory($condition)){
    		$result=array(
                'status'=>0,
                'data'=>'删除失败',
            );
    	}else{
    		$result['status']=1;
    		$result['data']='删除成功';
    	}

        $this->ajaxReturn($result);    	
    }

    public function ajaxDoAdd(){
        $result=array(
            'status'=>0,
            'data'=>'数据不全',
            );

        if(empty($_REQUEST['story_title']) || empty($_REQUEST['story_dev']) || empty($_REQUEST['story_inline_status']) || empty($_REQUEST['story_qat_tester'])){
            $this->ajaxReturn($result);
        }

        $data['story_id']=preg_match('/\d{4,}/',$_REQUEST['story_title'],$matches)? $matches[0]:'';
        $condition['story_id']=$data['story_id'];
        $data['story_title']=$_REQUEST['story_title'];
        $data['story_dev']=$_REQUEST['story_dev'];
        $data['story_add_time']=$data['story_innertest_begintime']=$data['story_crosstest_endtime']=$data['story_crosstest_begintime']=$data['story_innertest_endtime']=$data['story_outertest_begintime']=$data['story_outertest_endtime']=date('y-m-d h:i:s');
        $data['story_cross_tester']= isset($_REQUEST['story_cross_tester'])? $_REQUEST['story_cross_tester']:'';
        !empty($data['story_cross_tester'])?$data['story_is_coresstest']=1:$data['story_is_coresstest']=0;
        $data['story_crosstest_point']=$data['story_crosstest_error']=$data['story_innertest_point']=$data['story_innertest_error']=$data['story_outertest_point']=$data['story_outertest_error']='';
        $data['story_qat_tester']= isset($_REQUEST['story_qat_tester'])?$_REQUEST['story_qat_tester']:'';
        isset($_REQUEST['story_bug_type'])?$data['story_bug_type']=$_REQUEST['story_bug_type']:$data['story_bug_type']=2;
        isset($_REQUEST['story_inline_status'])?$data['story_inline_status']=$_REQUEST['story_inline_status']:0;
        isset($_REQUEST['story_comments'])?$data['story_comments']=$_REQUEST['story_comments']:$data['story_comments']='';

        if(D('Story')->getStories($condition)){
            $result=array(
                'status'=>0,
                'data'=>'需求已存在',
            );
        }

        if(!D('Story')->addStory($data)){
            $result=array(
                'status'=>0,
                'data'=>'插入失败',
            );
            $this->ajaxReturn($result);
        }

        $result=array(
                'status'=>1,
                'data'=>'插入成功',
            );
        $this->ajaxReturn($result);
    }

    public function doUpdate(){
        if(!empty($_REQUEST['id'])){
            $condition['id']=$_REQUEST['id'];
            $stories = D('Story')->getStories($condition,0,1);
        }

        $this->assign('story',$stories[0]);
        $this->assign('title','更新需求');
        $this->display('Story/update');
    }


    public function ajaxDoUpdateStory(){
        $reuslt=array(
            'status'=>0,
            'data'=>'数据不全',
            );
        if(empty($_REQUEST['story_id'])){
            $this->ajaxReturn($reuslt);
        }

        $condition['story_id']=$_REQUEST['story_id'];

        $data['story_inline_status']=$_REQUEST['story_inline_status'];
        $data['story_bug_type']=$_REQUEST['story_bug_type'];
        $data['story_comments']=$_REQUEST['story_comments'];

        if(!D('Story')->saveStory($condition,$data)){
            $reuslt=array(
                'status'=>0,
                'data'=>'更新失败',
            );
        }

        $reuslt=array(
            'status'=>1,
            'data'=>'更新成功',
        );

        $this->success($reuslt['data'],'index',2);
    }
}